package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.ProductConditionDTO;
import com.tulingxueyuan.mall.dto.ProductSaveParamsDTO;
import com.tulingxueyuan.mall.dto.ProductUpdateInitDTO;
import com.tulingxueyuan.mall.modules.pms.model.PmsProduct;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author Miao
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Resource
    PmsProductService pmsProductService;

    /**
     * 商品列表
     * Request URL: http://localhost:8099/product/list?pageNum=1&pageSize=5
     * Request Method: GET
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult list(ProductConditionDTO productConditionDTO){
        Page page=pmsProductService.list(productConditionDTO);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 上架状态更新
     * Request URL: http://localhost:8099/product/update/publishStatus?ids=24&publishStatus=0
     * Request Method: POST
     */
    @RequestMapping(value = "/update/publishStatus",method = RequestMethod.POST)
    public CommonResult updatePublishStatus(@RequestParam("ids") List<Long> ids,
                                            @RequestParam("publishStatus") Integer publishStatus){
        boolean result=pmsProductService.updateStatus(publishStatus,ids,PmsProduct::getPublishStatus);
        if(result) {
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 新品状态更新
     * Request URL: http://localhost:8099/product/update/newStatus?ids=24&newStatus=0
     * Request Method: POST
     */
    @RequestMapping(value = "/update/newStatus",method = RequestMethod.POST)
    public CommonResult updateNewStatus(@RequestParam("ids") List<Long> ids,
                                            @RequestParam("newStatus") Integer newStatus){
        boolean result=pmsProductService.updateStatus(newStatus,ids,PmsProduct::getNewStatus);
        if(result) {
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 推荐状态更新
     * Request URL: http://localhost:8099/product/update/recommendStatus?ids=24&recommendStatus=0
     * Request Method: POST
     */
    @RequestMapping(value = "/update/recommendStatus",method = RequestMethod.POST)
    public CommonResult updateRecommendStatus(@RequestParam("ids") List<Long> ids,
                                        @RequestParam("recommendStatus") Integer recommendStatus){
        boolean result=pmsProductService.updateStatus(recommendStatus,ids,PmsProduct::getRecommandStatus);
        if(result) {
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改商品
     * Request URL: http://localhost:8099/product/updateInfo/24
     * Request Method: GET
     */
    @RequestMapping(value = "/updateInfo/{id}",method = RequestMethod.GET)
    public CommonResult getUpdateInfo(@PathVariable Long id){
        ProductUpdateInitDTO productUpdateInitDTO=pmsProductService.getUpdateInfo(id);
        return CommonResult.success(productUpdateInitDTO);
    }

    /**
     * 更新商品信息
     * Request URL: http://localhost:8099/product/update/24
     * Request Method: POST
     */
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult update(@RequestBody @Valid ProductSaveParamsDTO productSaveParamsDTO){
        boolean result=pmsProductService.update(productSaveParamsDTO);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除商品
     * Request URL: http://localhost:8099/product/update/deleteStatus?ids=24&deleteStatus=1
     * Request Method: POST
     */
    @RequestMapping(value = "/update/deleteStatus",method = RequestMethod.POST)
    public CommonResult deleteStatus(@RequestParam("ids") List<Long> ids,
                                     @RequestParam("deleteStatus") Integer deleteStatus){
        boolean result=pmsProductService.deleteStatus(ids,deleteStatus);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 添加商品
     * Request URL: http://localhost:8099/product/create
     * Request Method: POST
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public CommonResult create(@RequestBody ProductSaveParamsDTO productSaveParamsDTO){
        boolean result=pmsProductService.create(productSaveParamsDTO);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }






}

